package org.openclinic.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.openclinic.domain.Appointment;
import org.openclinic.domain.Document;
import org.openclinic.domain.DocumentModel;
import org.openclinic.exception.AbstractExceptionHandler;
import org.openclinic.exception.DomainException;
import org.openclinic.service.AppointmentService;
import org.openclinic.service.DocumentModelService;
import org.openclinic.service.DocumentService;
import org.openclinic.to.AjaxResponse;
import org.openclinic.to.DataTablesTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes({ "document", "documentModel" })
public class DocumentController extends AbstractExceptionHandler {

	@Autowired
	private AppointmentService appointmentService;
	
	@Autowired
	private DocumentService documentService;
	
	@Autowired
	private DocumentModelService documentModelService;
	
	@RequestMapping(value = "/document/open.diag", method = RequestMethod.POST)
	public ModelAndView openAttestDialog(@RequestParam Long id, @RequestParam int documentType) {
		ModelAndView modelAndView = new ModelAndView("document-form.jsp");
		modelAndView.addObject("modelList", documentModelService.list());

		Document document = null;
		if (id == null) {
			modelAndView.addObject("document", document = new Document());
		} else {
			modelAndView.addObject("document", document = documentService.find(id));
		}
		document.setType(documentType);
		
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value = "/document/delete.action", method = RequestMethod.POST)
	public boolean deleteDocument(@RequestParam long id) {
		documentService.delete(id);
		return true;
	}
	
	@ResponseBody
	@RequestMapping(value = "/document/processModel.action", method = RequestMethod.POST)
	public AjaxResponse processModel(@RequestParam long modelId, @RequestParam long appointmentId) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat stf = new SimpleDateFormat("HH:mm");
		DocumentModel model = documentModelService.find(modelId);
		Appointment appointment = appointmentService.find(appointmentId);
		return new AjaxResponse(true,
			model.getDescription()
				.replaceAll("\\[Nome do Cliente\\]", appointment.getCustomer().getName())
				.replaceAll("\\[Data Atual\\]", sdf.format(new Date()))
				.replaceAll("\\[Início da Consulta\\]", stf.format(appointment.getStartDate()))
				.replaceAll("\\[Término da Consulta\\]", stf.format(appointment.getEndDate())));
	}
	
	@ResponseBody
	@RequestMapping(value = "/document/save.action", method = RequestMethod.POST)
	public Document save(@Valid @ModelAttribute Document document, BindingResult validationResult) {
		if(!validationResult.hasErrors()) {
			return documentService.save(document);
		}
		throw new DomainException(validationResult.getAllErrors());
	}
	
	// -- DOCUMENT MODEL -- //
	@ResponseBody
	@RequestMapping(value = "/document/model/delete.action", method = RequestMethod.POST)
	public boolean delete(@RequestParam long id) {
		documentModelService.delete(id);
		return true;
	}
	
	@RequestMapping(value = "/document/model.do", method = RequestMethod.GET)
	public String init() {
		return "document-model.view";
	}
	
	@RequestMapping(value = "/document/model/open.diag", method = RequestMethod.POST)
	public ModelAndView openDialog(@RequestParam Long id) {
		ModelAndView modelAndView = new ModelAndView("document-model-form.jsp");
		
		if (id == null) {
			modelAndView.addObject("documentModel", new DocumentModel());
		} else {
			modelAndView.addObject("documentModel", documentModelService.find(id));
		}
		
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value = "/document/model/list.action", method = RequestMethod.POST)
	public DataTablesTO<DocumentModel> list() {
		return new DataTablesTO<DocumentModel>(documentModelService.list());
	}
	
	@ResponseBody
	@RequestMapping(value = "/document/model/save.action", method = RequestMethod.POST)
	public DocumentModel save(@Valid @ModelAttribute DocumentModel documentModel, BindingResult validationResult) {
		if(!validationResult.hasErrors()) {
			documentModelService.save(documentModel);
			return documentModel;
		}
		throw new DomainException(validationResult.getAllErrors());
	}
	
}
